import { ref, onMounted, watch, computed } from "vue";
import { message } from "@/utils/message";
import {
  querySimulatorQuestionByType,
  findSimulatorQuestionById
} from "@/api/simulatorexam";
import { type FormExamProps } from "../utils/types";
import { ElMessageBox } from "element-plus";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import { useRouter } from "vue-router";

export function useRole() {
  const router = useRouter();

  const isPracticingQuestions = ref(true);
  const questionTypeRef = ref(0);
  const questionDataList = ref([]);
  const currentAnswerIdRef = ref(-1);
  const currentAnswerRef = ref(-1);
  const formExamData = ref<FormExamProps>({
    id: -1,
    questionid: "",
    type: -1,
    score: 0,
    childIndex: -1,
    childId: "",
    childType: -1
  });

  watch(currentAnswerRef, newValue => {
    if (newValue < 0) {
      return;
    }
    if (newValue >= questionDataList.value.length) {
      return;
    }

    const item = questionDataList.value[currentAnswerRef.value];
    findSimulatorQuestionById(item).then(res => {
      console.log(JSON.stringify(res));
      if (res.success) {
        currentAnswerIdRef.value = res.data.id;
        formExamData.value = res.data;
      } else {
        message("考试异常:" + res.errMsg, { type: "error" });
      }
    });
  });

  async function handleTypeChange(value) {
    console.log(value);
    querySimulatorQuestionByType({
      type: value
    })
      .then(res => {
        console.log(JSON.stringify(res));
        if (res.success) {
          questionDataList.value = res.data.list;
          currentAnswerRef.value = -1;
          setTimeout(function () {
            currentAnswerRef.value = 0;
          }, 300);
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  async function onPreQuestion() {
    if (currentAnswerRef.value <= 0) {
      return;
    }
    currentAnswerRef.value--;

    showIdRef.value = showMumber(currentAnswerRef.value);
  }

  async function onNextQuestion() {
    if (currentAnswerRef.value >= questionDataList.value.length - 1) {
      return;
    }
    currentAnswerRef.value++;

    showIdRef.value = showMumber(currentAnswerRef.value);
  }

  const multiTags = computed(() => {
    return useMultiTagsStoreHook()?.multiTags;
  });

  onMounted(() => {
    setTimeout(function () {
      if (isPracticingQuestions.value) {
        handleTypeChange(0);
      } else {
        ElMessageBox.confirm("是否进入刷题模式？", "系统提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info",
          dangerouslyUseHTMLString: true,
          draggable: true
        })
          .then(() => {
            handleTypeChange(0);
            isPracticingQuestions.value = true;
          })
          .catch(() => {
            isPracticingQuestions.value = false;
            useMultiTagsStoreHook().handleTags(
              "splice",
              "/training/question/index"
            );
            router.push({
              path: multiTags.value[(multiTags as any).value.length - 1].path
            });
          });
      }
    }, 300);
  });

  const showIdRef = ref("01");

  function showMumber(index) {
    if (index < 9) {
      return "0" + (index + 1);
    } else {
      return index + 1;
    }
  }

  return {
    isPracticingQuestions,
    questionTypeRef,
    handleTypeChange,
    questionDataList,
    onPreQuestion,
    onNextQuestion,
    currentAnswerIdRef,
    currentAnswerRef,
    formExamData,
    showIdRef,
    showMumber
  };
}
